package benoli.eu.examples.getPhotos;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ListView;

public class GetPhotos extends Activity {
	private ListView photoList;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        photoList = (ListView) findViewById(R.id.photoList);
        
        populateContactList();
    }

    private void populateContactList() {
        Cursor cursor = getContacts();
        
        String[] from = new String[] {
        		ContactsContract.Contacts.DISPLAY_NAME
        };
        
        int[] to = {
        		R.id.fullname
        };
        
        PhotoAdapter pa = new PhotoAdapter(this, R.layout.photo_item,
        		cursor, from, to);
        
        photoList.setAdapter(pa);
    }
    
    private Cursor getContacts() {
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME
        };
        
        String selection = "";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
        
        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }
}